<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Rss/classes/PhpFox_ServiceRss.class.php, $Author:  $ */



/** Service for working with rss
 *
 * @package module.rss
 */
class PhpFox_Mod_Rss_ServiceRss extends BaseService
{
	/**
	 * Get data for Blog RSS-view from 'jornal' table
	 *
	 * @return array: RSS item description. Look at RSS standards
	 */
	function getBlogsArray($iUserId = 0)
    {
	    $oItemBlog = App::getModuleItem('Blog','Blog');

	    $aCond   = array();
	    if ( $iUserId )
	    {
	    	$aCond[] = " user_id = '". $iUserId ."'";
	    }
	    $sSort   = 'time DESC';
	    $iLimit  = App::getParam('Mod_Rss.blogs_show_number');
	    $aLeftJoin = array();
	    $aLeftJoin[] = " ". App::getT('user') ." AS u ON(j.user_id = u.id) ";
	    list($aRes)  = $oItemBlog->getList($aCond,1,$iLimit,$sSort, '', '', 'j.id, j.text, j.title, j.time, u.user', $aLeftJoin);
	    App::loadClass('Text');
	    $oText = App::newObject('Text');
	    $aFinal = array();
	    $aFinal['rss_title'] = (($iUserId && isset($aRes[0]['user'])) ? " " . $aRes[0]['user'] . " :: " : "") . App::format('Mod_Rss.blogs');
	    $aFinal['rss_link'] = (($iUserId && isset($aRes[0]['user'])) ? App::makeUrl('public.user.blogs', array('name' => $aRes[0]['user'])) : App::makeUrl('public.blogs'));
	    foreach ($aRes as $aItem)
	    {
	        $aItem['link']        = $this->getLink('blog/view/id_'.$aItem['id']);
	        $aItem['guid']        = $this->getLink('blog/view/id_'.$aItem['id']);
	        $aItem['description'] = strip_tags($oText->parseText($aItem['text']));
	        $aItem['description'] = substr($this->unhtmlentities($aItem['description']), 0, 200);
	        $aItem['title']       = substr($aItem['title'], 0, 70);
	        $aItem['pubDate']  = date('r',$aItem['time']);
	        unset($aItem['time']);
	        unset($aItem['text']);
	        unset($aItem['id']);
	        unset($aItem['user']);
	        array_push($aFinal,$aItem);
	    }
        return $aFinal;
    }

	/**
	 * Get data for forums topic RSS-view from 'forum_topic' table
	 *
	 * @return array: RSS items description. Look at RSS standards
	 */
    function getThreadsArray($iId=0)
    {
	    $oItemForumTopic = App::getModuleItem('Forum','ForumTopic');

	    $aFields = array( 'id', 'title', 'user', 'text', 'time',);
	    $aCond   = array('type' => '='.$iId);
	    $sSort   = 'time DESC';
	    $iLimit  = App::getParam('Mod_Rss.threads_show_number');
	    list($aRes)  = $oItemForumTopic->getList($aCond,1,$iLimit,$sSort,$aFields);

	    App::loadClass('Text');
	    $oText = App::newObject('Text');
	    $aFinal = array();
	    $aFinal['rss_title'] = App::format('Mod_Rss.forums');
	    $aFinal['rss_link'] = App::makeUrl('public.forum');
	    foreach ($aRes as $aItem)
	    {
	        $aItem['link']        = $this->getLink('forum/posts/id_'.$aItem['id']);
	        $aItem['guid']        = $this->getLink('forum/posts/id_'.$aItem['id']);
	        $aItem['description'] = strip_tags($oText->parseText($aItem['text']));
	        $aItem['description'] = substr($this->unhtmlentities($aItem['description']), 0, 200);
	        $aItem['title']       = substr($aItem['title'], 0, 70);
	        $aItem['pubDate']  = date('r',$aItem['time']);
	        unset($aItem['time']);
	        unset($aItem['text']);
	        unset($aItem['id']);
	        unset($aItem['user']);
	        array_push($aFinal,$aItem);
	    }
	    return  $aFinal;
    }

	/**
	 * Get data for public events RSS-view from 'events' table
	 *
	 * @return array: RSS items description. Look at RSS standards
	 */
    function getEventsArray()
    {
	    $oItemEvent = App::getModuleItem('Event','Event');

	    $aFields = array( 'id', 'title', 'user', 'short', 'time',);
	    $aCond   = array('private' => '=1',
	                    );
	    $sSort   = 'time DESC';
	    $iLimit  = App::getParam('Mod_Rss.threads_show_number');
	    list($aRes)  = $oItemEvent->getList($aCond,1,$iLimit,$sSort,$aFields);

	    App::loadClass('Text');
	    $oText = App::newObject('Text');
	    $aFinal = array();
	    $aFinal['rss_title'] = App::format('Mod_Rss.events');
	    foreach ($aRes as $aItem)
	    {
	        $aItem['link']        = $this->getLink('event/view/id_'.$aItem['id']);
	        $aItem['guid']        = $this->getLink('event/view/id_'.$aItem['id']);
	        $aItem['description'] = strip_tags($oText->parseText($aItem['short']));
	        $aItem['description'] = substr($this->unhtmlentities($aItem['description']), 0, 200);
	        $aItem['title']       = substr($aItem['title'], 0, 70);
	        $aItem['pubDate']  = date('r',$aItem['time']);
	        unset($aItem['time']);
	        unset($aItem['short']);
	        unset($aItem['id']);
	        unset($aItem['user']);
	        array_push($aFinal,$aItem);
	    }
	    return  $aFinal;
    }

	/**
	 * Replace html special chars &...; with its meaning
	 *
	 * @param string $sString
	 * @return string: string with replased chars
	 */
    function unhtmlentities($sString)
    {
        $sString = str_replace('&nbsp;',' ',$sString);
        $sString = str_replace('&lt;','<',$sString);
        $sString = str_replace('&gt;','>',$sString);
        $sString = str_replace('&quot;','\'',$sString);
        $sString = str_replace('&amp;','&',$sString);
        App::loadClass('Text');
        $oTxt = new Text;
        $sString = $oTxt->utf8ToUnicode($sString);
        return $sString;
    }

    /**
     * Get data for forums posts RSS-view from 'forum' table
     *
     * @return array: RSS items description. Look at RSS standards
     */
    function getPostsArray($iId=0)
    {   	
    	$oSrvForum = App::getModuleService('Forum', 'Forum');
	    $oItmTopic = App::getModuleItem('Forum', 'ForumTopic');
	    $oItmReply = App::getModuleItem('Forum', 'ForumReply');

	    $oItmTopic->load($iId);
	    $sRssTitle = $this->unhtmlentities($oItmTopic->aData['title']);

	    $iLimit    = App::getParam('Mod_Rss.threads_show_number');
	    $iPageSize = App::getParam('public.pagesize');


        $aCond  = array('fid' => ' = "'.$iId.'"');
        list($aReply, $iTotal) = $oItmReply->getList($aCond);

        // adding 1 to match count including head post
        $iTotal++;

        $iPage = 1;
	    if ($iTotal > $iLimit)
	    {
	        $iPage = ceil($iTotal / $iLimit);
	    }

	    list($aPosts) = $oSrvForum->getPostsList($aCond, $iPage, $iLimit, '`time`');  
	    
	    if (count($aPosts) < $iLimit)
	    {
	        $aPosts = array_merge(array(0 => $oItmTopic->aData), $aPosts);
	    } 
	    else
	    {
	        for($i = 0; $i < count($aPosts); $i++)
    	    {
    	        $aPosts[$i]['real_page'] = ceil(($i + $iTotal - $iLimit + 1) / $iPageSize);
    	    }
	    }
	    
	    App::loadClass('Text');
	    $oText = App::newObject('Text');
	    $aFinal = array();
	    foreach ($aPosts as $aItem)
	    {
	        $iRealPage            = (isset($aItem['real_page']) and $aItem['real_page']) ? $aItem['real_page'] : 'last';
	        $aItem['link']        = $this->getLink('forum/posts/page_' . $iRealPage . '/id_'.$iId);
	        $aItem['description'] = strip_tags($oText->parseText($aItem['text']));
	        $aItem['description'] = substr($this->unhtmlentities($aItem['description']), 0, 200);
	        $aItem['title']       = $oItmTopic->aData['title'];
	        $aItem['pubDate']  = date('r',$aItem['time']);

	        $aFinal[] = array(
    	           'link'        => $aItem['link'],
    	           'description' => $aItem['description'],
    	           'title'       => $aItem['title'],
    	           'pubDate'     => $aItem['pubDate'],
    	        );
    	    }
            $aFinal['rss_title'] = App::format('Mod_Rss.forums') . " :: " . $sRssTitle;
            $aFinal['rss_link'] = App::makeUrl('forum.posts', array('id' => $oItmTopic->aData['id']));
    	    return  $aFinal;
    }

    /**
     * Gets link for RSS depending on server configuration
     *
     * @param string $sPath
     * @return string
     *
     *
     */
    function getLink($sPath)
    {
        $sLink = App::getParam('http') . App::getParam('host');

        if (!App::getParam('path_translated'))
        {
            $sLink .= 'index.php?' . App::getParam('get_path_var') . '=' . '/' . $sPath . '/';
        } else
        {
            if (!App::getParam('rewrite_engine'))
            {
                $sLink .= 'index.php/public/';
            }
            $sLink .= $sPath;
        }

        return $sLink;
    }
}

?>